#!/usr/bin/env python
# encoding: utf-8
"""
#-------------------------------------------------------------------#
#                   CONFIDENTIAL --- CUSTOM STUDIOS                 #     
#-------------------------------------------------------------------#
#                                                                   #
#                   @Project Name : Globallawonline                #
#                                                                   #
#                   @File Name    : checkynlaw.py                      #
#                                                                   #
#                   @Programmer   : 李建                            #
#                                                                   #  
#                   @Start Date   : 2021/2/20 9:51                 #
#                                                                   #
#                   @Last Update  : 2021/2/20 9:51                 #
#                                                                   #
#-------------------------------------------------------------------#
# Classes:该类用来检查印尼专家提供的法律的Excel记录与pdf文件的数量是否相同                                                          #
#                                                                   #
#-------------------------------------------------------------------#
"""
import os

import pandas as pd


class Sample:
    def __init__(self):
        pass
    def checkfile(self, excel_path, sheet_name, file_path):
        """
        检查excel记录是否有对应的pdf文件
        :param excel_path:Excle的全路径
        :param sheet_name:Excel的工作簿名
        :param file_path:pdf文件目录
        :return:
        """
        j = 0
        data = pd.read_excel(excel_path, sheet_name)
        for i in range(1,len(data)):
            value = str(data.iloc[i,10])
            if value == 'nan':
                continue
            Isright = False
            # pdf_name = value + '.pdf'
            # pdf_name1 = value.rstrip() + '.pdf'
            pdf_name = value
            pdf_name1 = value
            for root, dirs, files in os.walk(file_path, topdown=False):
                if pdf_name in files:
                    Isright = True
                    break
                if pdf_name1 in files:
                    Isright = True
                    break
            if not Isright:
                with open("nopdf.txt", 'a', encoding='utf-8') as f:
                    j = j+1
                    f.write(str(j) + ':' + str(data.iloc[i,0]) + ';' + pdf_name + '\n')


    def checkrecord(self, excel_path, sheet_name, file_path):
        """
        检查pdf文件是否有对应的excel记录
        :param excel_path:Excle的全路径
        :param sheet_name:Excel的工作簿名
        :param file_path:pdf文件目录
        :return:
        """
        j=0
        data = pd.read_excel(excel_path, sheet_name)
        pdf_files = list(data['SYS_FLD_DIGITFILENAME'])
        for root, dirs, files in os.walk(file_path, topdown=False):
            proot = root.lstrip(r'E:\工作记录\工作内容\“一带一路”项目\各国专家提供的法律资源资料\印度尼西亚')
            for file in files:
                Isright = False
                # pdf_filename = file.replace(".pdf", '')
                pdf_filename = file
                pdf_filename1 = pdf_filename.rstrip()
                if pdf_filename in pdf_files:
                    Isright = True
                if pdf_filename1 in pdf_files:
                    Isright = True
                if not Isright:
                    with open("norecord.txt", 'a', encoding='utf-8') as f:
                        j = j + 1
                        f.write(str(j) + ':'  + proot + '\\' + file + '\n')


if __name__ == '__main__':
    path = r'E:\工作记录\工作内容\“一带一路”项目\各国专家提供的法律资源资料\印度尼西亚'
    filepath = path + '\PP Indonesia1'
    excelpath = path + '\PP Indonesia1\Indonesian law bibliography data fields.xls'
    sheetname = 'example'
    # Sample().checkfile(excelpath,sheetname,filepath)
    Sample().checkrecord(excelpath,sheetname,filepath)